import 'package:jpush_flutter/jpush_flutter.dart';

class YuePush {
  YuePush._();

  late final JPush _jpush = JPush();

  static YuePush? _instance;

  static YuePush get instance {
    if (_instance == null) {
      _instance = YuePush._();
      _instance!.initPlatform();
    }
    return _instance!;
  }

  void initPlatform() {
    const bool isProduction = bool.fromEnvironment('dart.vm.product');

    try {
      _jpush.addEventHandler(
        onReceiveNotification: (message) async {
          print('onReceiveNotification - $message');
        },
        onOpenNotification: (message) async {
          print('onReceiveNotification- $message');
        },
        onReceiveMessage: (message) async {
          print('onReceiveMessage - $message');
        },
        onReceiveNotificationAuthorization: (message) async {
          print('onReceiveNotificationAuthorization - $message');
        },
      );
    } catch (e) {
      print(e);
    }

    _jpush.setup(
      appKey: '737bde199507dde123eb25cf',
      channel: 'developer-default',
      production: isProduction,
      debug: isProduction,
    );

    _jpush.applyPushAuthority(NotificationSettingsIOS(
      sound: true,
      alert: true,
      badge: true,
    ));
    _jpush
        .getRegistrationID()
        .then((value) => print('registration id : $value'));
  }
}
